<template>
	<view class="equity">
		<headerTop :userInfo="userInfo" @refrash="getAllList" :pageParams="pageParams"></headerTop>
		<view class="bottom-list">
			<view class="b-list-title">
				<up-line color="#a3a3a3" length="50rpx" margin="0rpx 20rpx"></up-line>
				<text>优惠券领取</text>
				<up-line margin="0rpx 20rpx" color="#a3a3a3" length="50rpx"></up-line>
			</view>
			<receiveList :pageParams="pageParams" @getTiket="getTiket"></receiveList>
		</view>
		<up-picker
			:show="show"
			@cancel="handleClose"
			:title="pageParams.pickerTitle"
			:columns="pageParams.columns"
			keyName="equityName"
			@confirm="confirm"
			@change="changeHandler"
		></up-picker>
	</view>
</template>

<script setup>
import { ref, reactive, onMounted, watchEffect, watch, computed, defineAsyncComponent, nextTick } from 'vue';
import { onLoad, onPullDownRefresh, onReady,onShow } from '@dcloudio/uni-app';
import { getALPhoneNumber, getWXphoneNumber } from '@/utils/login.js';
import dayjs from 'dayjs';
import { storeToRefs } from 'pinia';
import { useUserStore } from '@/store/user.js';
import initApi from '@/apis/init.js';
import headerTop from './equity/header.vue';
import receiveList from './equity/receiveList.vue';
// 或者使用配置参数来初始化，详情见文档
const dataR = ref();

const show = ref(false);
const { userInfo } = storeToRefs(useUserStore());
const pageParams = reactive({
	list: [],
	columns: [],
	equtyId: '',
	pickerTitle: ''
});
// const query = ref()
onLoad(async(query) => {
	console.log(query, 'query000');
	// #ifdef MP-ALIPAY
 await	getALPhoneNumber();
 console.log('执行完毕');
 	getAllList();
	// #endif
	// #ifdef MP-WEIXIN
	getWXphoneNumber();
	// #endif
});
onShow(()=>{
	setTimeout(() => {
	
	}, 1000);
})

// 状态数据

const confirm = (e) => {
	const { value } = e;
	console.log(value);
	show.value = false;
	initApi
		.getReceiveEquity({ ...value[0], id: pageParams.equtyId })
		.then((res) => {
			console.log(res);
			if (res.code == 20000) {
				getAllList();
				return uni.utils.toast('领取成功');
			} else {
				return uni.utils.toast(res.message);
			}
		})
		.catch((error) => {
			return uni.utils.toast(error.message);
		});
};
// 没有选择
const confirNoList = () => {
	initApi
		.getReceiveEquity({ id: pageParams.equtyId })
		.then((res) => {
			console.log(res);
			if (res.code == 20000) {
				getAllList();
				return uni.utils.toast('领取成功');
			} else {
				return uni.utils.toast(res.message);
			}
		})
		.catch((error) => {
			return uni.utils.toast(error.message);
		});
};
const handleClose = () => {
	show.value = false;
};
const changeHandler = (e) => {
	console.log(e, 'change');
};

const getTiket = (item) => {
	pageParams.equtyId = item.id;
	// 没有数据 就直接领取
	if (!item.equityPortfolioConfigList || item.equityPortfolioConfigList?.length < 1) {
		confirNoList()
		return;
	}
	// 弹框
	if (item.equityType == 1) {
		pageParams.pickerTitle = '选择银行立减金';
	} else if (item.equityType == 2) {
		pageParams.pickerTitle = '选择虚拟会员';
	}
	show.value = true;
	pageParams.columns.length = 0;
	pageParams.columns.push(item.equityPortfolioConfigList);
};

const getReceiveEquityBtn = () => {
	initApi.getReceiveEquity({}).then((res) => {
		console.log(res);
	});
};

const getUserInfo = () => {
	initApi
		.getUserInfo()
		.then((res) => {
			console.log(res);
			if (res.code == 20000) {
				userInfo.value.mobile = res.data.mobile;
				userInfo.value.username = res.data.username;
			}
		})
		.catch((error) => {});
};
const getAllList = () => {
	console.log("getAllList");
	// 获取page
	initApi.getReceiveEquityPage({ condition: {}, pageNum: 1, pageSize: 1000 }).then((res) => {
		if (res.code == 20000) {
			pageParams.list = res.data.records;
		}
	});
};
</script>

<style lang="scss" scoped>
@import url('@/static/scss/equity.scss');

.bottom-list {
	.b-list-title {
		height: 100rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		// background-color: red;
	}
}

::v-deep {
	image {
		vertical-align: bottom; // 解决image背景图片下方有10rpx 的白边
	}
}
</style>
